import { SUCCESS, BASE_URL } from '@/config'
import { Toast } from 'vant'

const headers = {
  Accept: 'application/json;charset=utf-8',
  'Content-Type': 'application/json;charset=utf-8'
}

export default function({ $axios, redirect, error }) {
  $axios.defaults.timeout = 80000

  $axios.defaults.headers = headers

  $axios.defaults.baseURL = BASE_URL

  $axios.interceptors.request.use(
    config => {
      return config
    },
    error => {
      Promise.reject(error)
    }
  )
  $axios.interceptors.response.use(
    response => {
      if (response.data && response.data.code === SUCCESS) {
        return response.data
      } else {
        // error({
        //   message: response.data.msg,
        //   statusCode: response.statusCode
        // })

        if (!process.server) {
          Toast(response.data.msg)
        }

        // return Promise.reject(response.data)
      }
    },
    error => {
      redirect('/400')
      return Promise.reject(error)
    }
  )
}
